package com.xltq.controller;

import com.xltq.entity.Address;
import com.xltq.entity.Cart;
import com.xltq.entity.Orders;
import com.xltq.entity.User;
import com.xltq.service.IAddressService;
import com.xltq.service.ICartService;
import com.xltq.service.IOrdersService;
import com.xltq.service.impl.IAddressServiceImpl;
import com.xltq.service.impl.ICartServiceImpl;
import com.xltq.service.impl.IOrdersServiceImpl;
import com.xltq.utils.Constants;
import org.omg.PortableInterceptor.INACTIVE;

import javax.lang.model.element.VariableElement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

//订单controller
@WebServlet("/order")
public class OrdersController extends BaseServlet {
    //用户订单展示页面(在购物车中添加收货地址之后的信息展示)
   public String preView(HttpServletRequest request, HttpServletResponse response) throws Exception {
               //1.获取用户的uid
              String uid = request.getParameter("uid");
              //2.调用地址和购物车 service层获取用户的 地址和购物车信息
              IAddressService addressService = new IAddressServiceImpl();
              List<Address> addressList = addressService.findAddressByUid(Integer.valueOf(uid));
              ICartService  cartService = new ICartServiceImpl();
              List<Cart> cartList = cartService.findAll(Integer.valueOf(uid));
              //3.将查询到数据存储到作用域中,传递到订单页面
                request.setAttribute("addressList", addressList);
                request.setAttribute("cartList", cartList);
                return Constants.FORWARD+"/order.jsp";
   }
    //创建订单
    public String create(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.获取请求参数
        String uid = request.getParameter("uid");
        String aid = request.getParameter("aid");
        String sum = request.getParameter("sum");
        //2.调用业务层生成订单
        IOrdersService ordersService = new IOrdersServiceImpl();
        ordersService.createOrder(aid, uid, sum);
        //3.跳转到一个Servlet中方法由这个方法统一处理展示效果
        return  Constants.FORWARD+"order?method=show";

    }
    //展示订单信息
    public String show(HttpServletRequest request, HttpServletResponse response) throws Exception {
         //1.验证是否登录,如果登录可以查看,否者不允许查看直接跳转到登录页面
        HttpSession session = request.getSession();
        User loginUser = (User)session.getAttribute("loginUser");
        if(Objects.isNull(loginUser)){
            session.setAttribute("msg", "登录后才可以查看订单信息!");
            return Constants.REDIRECT+"/login.jsp";
        }
        IOrdersService ordersService = new IOrdersServiceImpl();
        List<Orders> ordersList = ordersService.findOrdersByUid(loginUser.getUid());
        request.setAttribute("ordersList", ordersList);
        return Constants.FORWARD+"/orderList.jsp";

    }
    //订单详情展示
    public String detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String oid = request.getParameter("oid");
        IOrdersService ordersService = new IOrdersServiceImpl();
        Orders orders = ordersService.findOrdersByOid(oid);
        request.setAttribute("order",orders);
        return  Constants.FORWARD+"/orderDetail.jsp";
    }
    //接受接口回调信息操作,更新订单状态
    public String success(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.收参数
        String oid = request.getParameter("oid");
        String result = request.getParameter("result");
        //判断result决定订单状态
        if(Objects.nonNull(result) && result.equalsIgnoreCase("SUCCESS")){
            //更新订单状态并跳转到订单展示页面
            IOrdersService ordersService = new IOrdersServiceImpl();
            ordersService.updateStateByOid(oid, "2");
            return  Constants.FORWARD+"/order?method=show";
        }else{
            //支付失败
            request.getSession().setAttribute("msg", "订单"+oid+"支付失败!");
            //跳转奥信息页面
            return  Constants.REDIRECT+"/message.jsp";
        }


    }


}
